package com.castle.fortress.admin.oa.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.annotation.CastleLog;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.oa.entity.AttendanceSettingEntity;
import com.castle.fortress.admin.oa.dto.AttendanceSettingDto;
import com.castle.fortress.admin.oa.enums.AttendanceTypeEnum;
import com.castle.fortress.admin.oa.service.AttendanceSettingService;
import com.castle.fortress.common.enums.OperationTypeEnum;
import com.castle.fortress.common.utils.ConvertUtil;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
import org.apache.logging.log4j.util.Strings;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.castle.fortress.admin.utils.ExcelUtils;

import javax.servlet.http.HttpServletResponse;

import com.castle.fortress.common.entity.DynamicExcelEntity;

import java.util.List;

/**
 * 考勤规则设置 控制器
 *
 * @author whc
 * @since 2022-11-03
 */
@Api(tags = "考勤规则设置管理控制器")
@Controller
public class AttendanceSettingController {
    @Autowired
    private AttendanceSettingService attendanceSettingService;

    /**
     * 考勤规则设置的分页展示
     *
     * @param attendanceSettingDto 考勤规则设置实体类
     * @param current              当前页
     * @param size                 每页记录数
     * @return
     */
    @CastleLog(operLocation = "考勤规则设置分页展示", operType = OperationTypeEnum.QUERY)
    @ApiOperation("考勤规则设置分页展示")
    @GetMapping("/oa/attendanceSetting/page")
    @ResponseBody
    @RequiresPermissions("oa:attendanceSetting:pageList")
    public RespBody<IPage<AttendanceSettingDto>> pageAttendanceSetting(AttendanceSettingDto attendanceSettingDto, @RequestParam(required = false) Integer current, @RequestParam(required = false) Integer size) {
        Integer pageIndex = current == null ? GlobalConstants.DEFAULT_PAGE_INDEX : current;
        Integer pageSize = size == null ? GlobalConstants.DEFAULT_PAGE_SIZE : size;
        Page<AttendanceSettingDto> page = new Page(pageIndex, pageSize);
        IPage<AttendanceSettingDto> pages = attendanceSettingService.pageAttendanceSettingExtends(page, attendanceSettingDto);

        return RespBody.data(pages);
    }

    /**
     * 考勤规则设置的列表展示
     *
     * @param attendanceSettingDto 考勤规则设置实体类
     * @return
     */
    @CastleLog(operLocation = "考勤规则设置列表展示", operType = OperationTypeEnum.QUERY)
    @ApiOperation("考勤规则设置列表展示")
    @GetMapping("/oa/attendanceSetting/list")
    @ResponseBody
    public RespBody<List<AttendanceSettingDto>> listAttendanceSetting(AttendanceSettingDto attendanceSettingDto) {
        List<AttendanceSettingDto> list = attendanceSettingService.listAttendanceSetting(attendanceSettingDto);
        return RespBody.data(list);
    }

    /**
     * 考勤规则设置保存
     *
     * @param attendanceSettingDto 考勤规则设置实体类
     * @return
     */
    @CastleLog(operLocation = "考勤规则设置保存", operType = OperationTypeEnum.INSERT)
    @ApiOperation("考勤规则设置保存")
    @PostMapping("/oa/attendanceSetting/save")
    @ResponseBody
    @RequiresPermissions("oa:attendanceSetting:save")
    public RespBody<String> saveAttendanceSetting(@RequestBody AttendanceSettingDto attendanceSettingDto) {
        if (attendanceSettingDto == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if (AttendanceTypeEnum.COMMUTE.getCode().equals(attendanceSettingDto.getType())) {
            if ("".equals(attendanceSettingDto.getPersonnel()) || "SPLIT".equals(attendanceSettingDto.getPersonnel())) {
                throw new BizException("需要选择打卡部门/人员");
            }
            if ("".equals(attendanceSettingDto.getAddressRange())) {
                throw new BizException("需要选择考勤地点");
            }
        }
        AttendanceSettingEntity attendanceSettingEntity = ConvertUtil.transformObj(attendanceSettingDto, AttendanceSettingEntity.class);
        if (attendanceSettingService.save(attendanceSettingEntity)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 考勤规则设置编辑
     *
     * @param attendanceSettingDto 考勤规则设置实体类
     * @return
     */
    @CastleLog(operLocation = "考勤规则设置编辑", operType = OperationTypeEnum.UPDATE)
    @ApiOperation("考勤规则设置编辑")
    @PostMapping("/oa/attendanceSetting/edit")
    @ResponseBody
    @RequiresPermissions("oa:attendanceSetting:edit")
    public RespBody<String> updateAttendanceSetting(@RequestBody AttendanceSettingDto attendanceSettingDto) {
        if (attendanceSettingDto == null || attendanceSettingDto.getId() == null || attendanceSettingDto.getId().equals(0L)) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if (AttendanceTypeEnum.COMMUTE.getCode().equals(attendanceSettingDto.getType())) {
            if ("".equals(attendanceSettingDto.getPersonnel()) || "SPLIT".equals(attendanceSettingDto.getPersonnel())) {
                throw new BizException("需要选择打卡部门/人员");
            }
            if ("".equals(attendanceSettingDto.getAddressRange())) {
                throw new BizException("需要选择考勤地点");
            }
        }
        AttendanceSettingEntity attendanceSettingEntity = ConvertUtil.transformObj(attendanceSettingDto, AttendanceSettingEntity.class);
        if (attendanceSettingService.updateById(attendanceSettingEntity)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 考勤规则设置删除
     *
     * @param id
     * @return
     */
    @CastleLog(operLocation = "考勤规则设置删除", operType = OperationTypeEnum.DELETE)
    @ApiOperation("考勤规则设置删除")
    @PostMapping("/oa/attendanceSetting/delete")
    @ResponseBody
    @RequiresPermissions("oa:attendanceSetting:delete")
    public RespBody<String> deleteAttendanceSetting(@RequestParam Long id) {
        if (id == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if (attendanceSettingService.removeById(id)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }


    /**
     * 考勤规则设置批量删除
     *
     * @param ids
     * @return
     */
    @CastleLog(operLocation = "考勤规则设置批量删除", operType = OperationTypeEnum.DELETE)
    @ApiOperation("考勤规则设置批量删除")
    @PostMapping("/oa/attendanceSetting/deleteBatch")
    @ResponseBody
    @RequiresPermissions("oa:attendanceSetting:deleteBatch")
    public RespBody<String> deleteAttendanceSettingBatch(@RequestBody List<Long> ids) {
        if (ids == null || ids.size() < 1) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if (attendanceSettingService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        } else {
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 考勤规则设置详情
     *
     * @param id 考勤规则设置id
     * @return
     */
    @CastleLog(operLocation = "考勤规则设置详情", operType = OperationTypeEnum.QUERY)
    @ApiOperation("考勤规则设置详情")
    @GetMapping("/oa/attendanceSetting/info")
    @ResponseBody
    @RequiresPermissions("oa:attendanceSetting:info")
    public RespBody<AttendanceSettingDto> infoAttendanceSetting(@RequestParam Long id) {
        if (id == null) {
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        AttendanceSettingDto attendanceSettingDto = attendanceSettingService.getByIdExtends(id);

        return RespBody.data(attendanceSettingDto);
    }

    /**
     * 动态表头导出 依据展示的字段导出对应报表
     *
     * @param dynamicExcelEntity
     * @param response
     * @throws Exception
     */
    @CastleLog(operLocation = "考勤规则设置导出", operType = OperationTypeEnum.EXPORT)
    @PostMapping("/oa/attendanceSetting/exportDynamic")
    @ApiOperation("动态表头导出，依据展示的字段导出对应报表")
    public void exportDynamic(@RequestBody DynamicExcelEntity<AttendanceSettingDto> dynamicExcelEntity, HttpServletResponse response) throws Exception {
        List<AttendanceSettingDto> list = attendanceSettingService.listAttendanceSetting(dynamicExcelEntity.getDto());
        //字典、枚举、接口、json常量等转义后的列表数据 根据实际情况初始化该对象，null为list数据直接导出
        List<List<Object>> dataList = null;
        /**
         * 根据实际情况初始化dataList,可参照 com.castle.fortress.admin.system.controller.TmpDemoController类中方法：exportDynamic
         */
        ExcelUtils.exportDynamic(response, dynamicExcelEntity.getFileName() + ".xlsx", null, list, dynamicExcelEntity.getHeaderList(), dataList);
    }


}
